// pages/userAccount/userAccount.js
const accountService = require('../../service/accountService.js');
const utils = require('../../utils/util.js');
// load promise 
require('../../utils/wx-pro.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    deposit: 0,
    money: '',
    rechargeList: [
      {
        key: '1',
        value: 0.01
      },
      {
        key: '2',
        value: 300
      },
      {
        key: '3',
        value: 500
      },
      {
        key: '4',
        value: 1000
      }
    ],
    payDelay: true,
    showToast: false,
    toastMsg: ''
  },

  //错误提示
  showToastFuc(msg) {
    this.setData({
      showToast: true,
      toastMsg: msg
    })
    setTimeout(() => {
      this.setData({
        showToast: false,
        toastMsg: ''
      })
    }, 2000)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserAccount()
  },

  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      money: e.detail.value,
      payDelay: false
    })
  },
  
  getUserAccount() {
    accountService.getTotalCash().then(data => {
      if (data.code == 200) {
        this.setData({
          deposit: data.response.deposit.preDeposit
        })
      } else {
        this.showToastFuc(data.message)
      }
    })
    .catch(() => {
      console.log('error')
    })
  },
  confirmPay(){
    var params = { payType: 1, money: this.data.money }
    accountService.depositCharge(params).then(data => {
      if (data.code == 200) {
        wx.requestPayment({
          'timeStamp': data.response.payInfo.timeStamp,
          'nonceStr': data.response.payInfo.nonceStr,
          'package': data.response.payInfo.package,
          'signType': data.response.payInfo.signType,
          'paySign': data.response.payInfo.paySign,
          'success': function (res) {
            wx.showToast({
              title: '支付成功',
              icon: 'succes',
              duration: 1000
            })
            console.log(res)
          },
          'fail': function (res) {
            console.log(res)
            if (res.errMsg == 'requestPayment:fail cancel'){
              wx.showToast({
                title: '取消支付',
                icon: 'succes',
                duration: 1000
              })
            }
          }
        })

      } else {
        this.showToastFuc(data.message)
      }
    })
    .catch(() => {
      console.log('error')
    })
  }
})